package net.jarang.android.saleoffline.extention;

import android.content.Context;
import android.hardware.SensorManager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.Gallery;

public class GalleryFling extends Gallery {
	private float mDeceleration;
	private float mDistanceX;

	public GalleryFling(Context context) {
		super(context);
	}
	
	public GalleryFling(Context context, AttributeSet attrs) {
		super(context, attrs);
		float ppi = context.getResources().getDisplayMetrics().density * 160.0f;
		mDeceleration = SensorManager.GRAVITY_EARTH * 39.37f * ppi * ViewConfiguration.getScrollFriction();
	}
	
	@Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
	    float distanceY) {
	mDistanceX += distanceX;
	return super.onScroll(e1, e2, distanceX, distanceY);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
	if (event.getAction() == MotionEvent.ACTION_DOWN) {
	    mDistanceX = 0;
	}
	return super.onTouchEvent(event);
    }	
	
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		float toMoveDistance = getWidth() - Math.abs(mDistanceX);
		float maxVelocity = (float) Math.sqrt(toMoveDistance * mDeceleration
			* 2);
		float revisedVelocityX = 0;

		if (velocityX > 0) {
		    revisedVelocityX = Math.min(velocityX, maxVelocity);
		} else {
		    revisedVelocityX = Math.max(velocityX, -maxVelocity);
		}

		return super.onFling(e1, e2, revisedVelocityX, velocityY);
	}
}
